package com.matrix.tool.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import com.matrix.tool.R
import kotlin.math.min

class CircleTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    View(context, attrs, defStyleAttr) {
    private var titleText: String? = ""

    /**     * 字体颜色      */
    private var titleColor = Color.BLACK

    /**     * 背景颜色,设置默认颜色      */
    private var backColor = Color.GRAY

    /**     * 色值      */
    private var titleSize = 0

    /**     *控制文本的范围      */
    private var mPaint: Paint= Paint()
    private var mRect: Rect? = null
    private var mBound: Rect? = null

    init {
        init(context,attrs,defStyleAttr)
        mPaint.textSize = titleSize.toFloat()
        mBound =Rect()
        titleText?.let { mPaint.getTextBounds(titleText, 0, it.length, mBound) }
    }

    private fun init(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) {
        val a: TypedArray = context.getTheme()
            .obtainStyledAttributes(attrs, R.styleable.CustomCircleView, defStyleAttr, 0)
        val n: kotlin.Int = a.getIndexCount()
        for (i in 0..<n) {
            val attr: kotlin.Int = a.getIndex(i)
            when (attr) {
                R.styleable.CustomCircleView_titleText -> titleText = a.getString(attr)
                R.styleable.CustomCircleView_titleTextColor -> titleColor =
                    a.getColor(attr, android.graphics.Color.BLACK)

                R.styleable.CustomCircleView_backColor -> backColor =
                    a.getColor(attr, android.graphics.Color.GREEN)

                R.styleable.CustomCircleView_titleTextSize -> titleSize = a.getDimensionPixelSize(
                    attr, android.util.TypedValue.applyDimension(
                        android.util.TypedValue.COMPLEX_UNIT_SP,
                        16f,
                        getResources().getDisplayMetrics()
                    ).toInt()
                )
            }
        }
        a.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = getMySize(100, widthMeasureSpec)
        val height = getMySize(100, heightMeasureSpec)
        setMeasuredDimension(width, height)
    }

    /**
     * 计算相应的宽高
     * @param defaultSize 默认值
     * @param measureSpec
     * @return
     */
    private fun getMySize(defaultSize: Int, measureSpec: Int): Int {
        var mySize = defaultSize
        val mode = MeasureSpec.getMode(measureSpec)
        val size = MeasureSpec.getSize(measureSpec)

        when (mode) {
            MeasureSpec.UNSPECIFIED -> {
                //如果没有指定大小，就设置为默认大小
                mySize = defaultSize
            }

            MeasureSpec.AT_MOST -> {
                //如果测量模式是最大取值为size
                //我们将自己计算View的大小
                mPaint!!.textSize = titleSize.toFloat()
                mPaint!!.getTextBounds(titleText, 0, titleText!!.length, mBound)
                val textWidth = mBound!!.width().toFloat()
                val desired = (paddingLeft + textWidth + paddingRight).toInt()
                mySize = desired
            }

            MeasureSpec.EXACTLY -> {
                //如果是固定的大小，那就不要去改变它
                mySize = size
            }
        }
        return mySize
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //圆的半径
        val r = min((measuredWidth / 2).toDouble(), (measuredHeight / 2).toDouble()).toInt()
        //圆心的横坐标
        val centerX = r
        //圆心的纵坐标
        val centerY = r
        val paint = Paint()
        paint.color = Color.GREEN
        mPaint!!.color = backColor
        //绘制圆
        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), r.toFloat(), mPaint!!)
        mPaint!!.color = titleColor
        //绘制Text
        canvas.drawText(
            titleText!!,
            (width / 2 - mBound!!.width() / 2).toFloat(),
            (height / 2 + mBound!!.height() / 2).toFloat(),
            mPaint!!
        )
    }

    fun setTitleText(str: String) {
        titleText = str
        invalidate()
    }

    fun setBackColor(color: Int) {
        backColor = color
        invalidate()
    }
}